package org.aaron.demos;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ListViewActivity extends ListActivity {
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewlayout);
		initListViewDate();
	}

	private void initListViewDate() {
		listView = getListView();
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		Map<String, String> mapOne = new HashMap<String, String>();
		mapOne.put("user_ip", "192.168.1.88");
		mapOne.put("user_name", "aaron");
		Map<String, String> mapTow = new HashMap<String, String>();
		mapTow.put("user_ip", "192.168.1.188");
		mapTow.put("user_name", "weilong");
		data.add(mapOne);
		data.add(mapTow);
		ListAdapter adapter = new SimpleAdapter(this, data, R.layout.listviewitem, new String[] { "user_ip", "user_name" }, new int[] { R.id.user_ip, R.id.user_name });
		this.setListAdapter(adapter);

		// 代码添加动画效果
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.list_anim);
		animation.setAnimationListener(new myAnimationListener());
		LayoutAnimationController layoutAnimationController = new LayoutAnimationController(animation);
		layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
		listView.setLayoutAnimation(layoutAnimationController);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT);
	}

	class myAnimationListener implements AnimationListener {

		@Override
		public void onAnimationEnd(Animation animation) {
			Log.d("ListViewActivity myAnimationListener", "onAnimationEnd");
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			Log.d("ListViewActivity myAnimationListener", "onAnimationRepeat");

		}

		@Override
		public void onAnimationStart(Animation animation) {
			Log.d("ListViewActivity myAnimationListener", "onAnimationStart");

		}

	}

}
